//
//  YFFullCamarmView.m
//  PYQDemo
//
//  Created by Coollang on 2017/9/13.
//  Copyright © 2017年 coollang. All rights reserved.
//

#import "YFFullCamarmPlayView.h"

@interface YFFullCamarmPlayView()
// 外界圆形
@property (nonatomic, strong) CAShapeLayer *whiteCircleLayer;
// 内部圆形
@property (nonatomic, strong) CAShapeLayer *circleLayer;
// 进度扇形
@property (nonatomic, strong) CAShapeLayer *progressLayer;
@property (assign, nonatomic) CGFloat currentProgress;

// 选中状态内部方形
@property (nonatomic, strong) CAShapeLayer *selectRoundLayer;

@property (nonatomic, strong) UILongPressGestureRecognizer *longGes;


@end

@implementation YFFullCamarmPlayView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
       
        UITapGestureRecognizer *playViewTapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playViewTapGrEvent:)];
        [self addGestureRecognizer:playViewTapGr];
        self.tapGes = playViewTapGr;
        [self addLongGer];
        [self setup];
    }
    return self;
}

- (void)setSelecte:(BOOL)selecte {
    _selecte = selecte;
    if (self.isNeedAddLongGer) {
        self.selectRoundLayer.hidden = YES;
        self.circleLayer.hidden = selecte;
    }else {
        self.circleLayer.hidden = selecte;
        self.selectRoundLayer.hidden = !selecte;
    }
}
- (void)setIsNeedAddLongGer:(BOOL)isNeedAddLongGer {
    _isNeedAddLongGer = isNeedAddLongGer;
    self.longGes.enabled = isNeedAddLongGer;
    self.circleLayer.hidden = isNeedAddLongGer;
}

- (void)setCircleLayerColor:(UIColor *)circleLayerColor {
    _circleLayerColor = circleLayerColor;
    self.circleLayer.fillColor = circleLayerColor.CGColor;
}

- (void)addLongGer {
    UILongPressGestureRecognizer *playViewLongPrg = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(playViewLongPgrEvent:)];
    playViewLongPrg.minimumPressDuration = 0.25;
    self.longGes = playViewLongPrg;
    self.longGes.enabled = NO;
    [self addGestureRecognizer:playViewLongPrg];
}

- (void)playViewLongPgrEvent:(UILongPressGestureRecognizer *)longPgr  {
    if ([self.delegate respondsToSelector:@selector(longProessWithFullCamarmPlayView:PgrEvent:)]) {
        [self.delegate longProessWithFullCamarmPlayView:self PgrEvent:longPgr];
    }
    if (longPgr.state == UIGestureRecognizerStateCancelled || longPgr.state == UIGestureRecognizerStateEnded) {
        [self clean];
    }
}

- (void)playViewTapGrEvent:(UITapGestureRecognizer *)gesture {
    if ([self.delegate respondsToSelector:@selector(tapPlayViewWithFullCamarmPlayView:)]) {
        [self.delegate tapPlayViewWithFullCamarmPlayView:self];
    }
}

- (void)setup {
    self.backgroundColor = [UIColor clearColor];
    CAShapeLayer *whiteCircleLayer = [CAShapeLayer layer];
    whiteCircleLayer.strokeColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1].CGColor;
    whiteCircleLayer.fillColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0].CGColor;
    whiteCircleLayer.lineWidth = 5;
    whiteCircleLayer.path = [self circlePath:self.frame.size.width * 0.5].CGPath;
    [self.layer addSublayer:whiteCircleLayer];
    self.whiteCircleLayer = whiteCircleLayer;
    
    CAShapeLayer *circleLayer = [CAShapeLayer layer];
    circleLayer.strokeColor = [UIColor clearColor].CGColor;
    circleLayer.fillColor = self.circleLayerColor == nil ? [UIColor redColor].CGColor : self.circleLayerColor.CGColor;
    circleLayer.path = [self circlePath:(self.frame.size.width * 0.5 - 6)].CGPath;
    [self.layer addSublayer:circleLayer];
    circleLayer.hidden = NO;
    
    self.circleLayer = circleLayer;
    
    CAShapeLayer *roundLayer = [CAShapeLayer layer];
    roundLayer.strokeColor = [UIColor clearColor].CGColor;
    roundLayer.fillColor = [UIColor redColor].CGColor;
    roundLayer.path = [self cornerView].CGPath;
    [self.layer addSublayer:roundLayer];
    roundLayer.hidden = YES;
    self.selectRoundLayer = roundLayer;
    
    if (self.isNeedAddLongGer == YES) {
        self.selectRoundLayer.hidden = !self.selecte;
        self.circleLayer.hidden = !self.selecte;
    }
    CAShapeLayer *progressLayer = [CAShapeLayer layer];
    progressLayer.strokeColor = [UIColor colorWithRed:253/255.0 green:142/255.0 blue:36/255.0 alpha:1].CGColor;
    progressLayer.fillColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0].CGColor;
    progressLayer.lineWidth = 5;
    progressLayer.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5) radius:self.frame.size.width * 0.5 startAngle:-M_PI / 2 endAngle:-M_PI / 2 + M_PI * 2 * 1 clockwise:true].CGPath;
    progressLayer.hidden = YES;
    [self.layer addSublayer:progressLayer];
    self.progressLayer = progressLayer;
    self.currentProgress = 0.f;
}

- (UIBezierPath *)circlePath:(CGFloat)radius {
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5) radius:radius startAngle:0 endAngle:M_PI * 2 clockwise:true];
    path.lineWidth = 1;
    return path;
}

- (UIBezierPath *)cornerView {
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake((self.frame.size.width - 25)*0.5, (self.frame.size.height - 25)*0.5, 25, 25) byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(4, 4)];
    return path;
}


- (void)clean {
    self.progressLayer.hidden = YES;
    if (self.isNeedAddLongGer == YES) {
        self.circleLayer.hidden = YES;
    }
    self.currentProgress = 0;
}

- (void)setProgress:(CGFloat)progress {
    _progress = progress;
    self.progressLayer.hidden = NO;
    if (self.isNeedAddLongGer) {
        self.circleLayer.hidden = NO;
    }
    CABasicAnimation *circleAnim = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    circleAnim.fromValue = @(self.currentProgress);
    circleAnim.toValue = @(progress);
    circleAnim.duration = 1.0f;
    circleAnim.fillMode = kCAFillModeForwards;
    circleAnim.removedOnCompletion = NO;
    circleAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [self.progressLayer addAnimation:circleAnim forKey:@"circle"];
    self.currentProgress = progress;
}

- (UIBezierPath *)pathForProgress:(CGFloat)progress {
    CGPoint center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
    CGFloat radius = self.frame.size.height * 0.5;
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:-M_PI / 2 endAngle:-M_PI / 2 + M_PI * 2 * progress clockwise:true];
    path.lineWidth = 1;
    return path;
}

@end
